-- newJewelPropRender

local ClassRef = app.mvc.createRenderClass(...)

function ClassRef.create(node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.Tips_JewelRender))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init()
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

    self:setContentSize( self._ui["Panel"]:getContentSize() )

	self._slotBg_1 = self._ui["Panel/bg_1"] -- 珠宝品质背景框
	self._slotBg_2 = self._ui["Panel/bg_2"] -- 镶嵌槽bg
	self._slotIcon = self._ui["Panel/icon"] -- 镶嵌的珠宝icon
	self._slotAttrName = self._ui["Panel/data_tlt"]
	self._slotAttrNum = self._ui["Panel/data_num"]
	self._slotStrengthenLv = self._ui["Panel/num"]

	self:refresh()
end

-- data = jewelSlot
function ClassRef:refresh()
	if self._data then
		self:setVisible( true )

        local jewelSlotBg -- 珠宝品质背景框
        local jewelBg = self._data:getIcon() -- 镶嵌槽bg
        local jewelIcon -- 镶嵌的珠宝icon
        local strengthenLv -- 镶嵌珠宝强化等级
        local jewelQuality -- 镶嵌珠宝提供的品质
        local jewelAttrName -- 镶嵌珠宝提供的属性名字
        local jewelAttrNum -- 镶嵌珠宝提供的属性值

        if self._data:hasJewel() then -- 镶嵌有宝石？
            local jewel = self._data.jewel
            jewelSlotBg = jewel:getSmallIconBg()
            jewelIcon = jewel:getSmallIcon()
--            strengthenLv = jewel:getLevel()
            -- 客户端将宝石等级主动+1
            strengthenLv = jewel:getLevelPlus1()
            jewelQuality = jewel:getQuality()
            jewelAttrName, jewelAttrNum = UD:formatAttr( jewel:getAttrType(), jewel:getAttrVal() )
        end

        if jewelSlotBg then
            self._slotBg_1:setVisible( true )
            self._slotBg_1:loadTexture( jewelSlotBg )
        else
            self._slotBg_1:setVisible( false )
        end
        if jewelBg then
            self._slotBg_2:setVisible( not jewelSlotBg )
            self._slotBg_2:loadTexture( jewelBg )
        else
            self._slotBg_2:setVisible( false )
        end
        if jewelIcon then
            self._slotIcon:setVisible( true )
            self._slotIcon:loadTexture( jewelIcon )
        else
            self._slotIcon:setVisible( false )
        end
        self._slotAttrName:setString( jewelAttrName and jewelAttrName or L("lua_code_text_500") )
        self._slotAttrNum:setString( jewelAttrNum and jewelAttrNum or "" )
        self._slotStrengthenLv:setVisible( strengthenLv and strengthenLv > 0 or false )
        self._slotStrengthenLv:setString( strengthenLv and strengthenLv or "" )

        if jewelAttrName then -- 有属性？
            if jewelQuality then
                self._slotAttrName:setTextColor( Enums.QualityColor[ jewelQuality ] )
                self._slotAttrNum:setColor( Enums.QualityColor[ jewelQuality ] )
            else
                self._slotAttrName:setTextColor( display.COLOR_WHITE )
                self._slotAttrNum:setColor( display.COLOR_WHITE )
            end
        else
            local defaultColor = cc.c3b( 68, 68, 68 )
            self._slotAttrName:setTextColor( defaultColor )
            self._slotAttrNum:setColor( defaultColor )
        end
	else
		self:setVisible( false )
	end
end

return ClassRef
